



import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;


public class ReadLanacion {

	/**
	 * @param args
	 */
	private static final String ITEMAUTO_XPATH = "//div[@class='itemAutos']";
	public static void main(String[] args) throws XPatherException, MalformedURLException, IOException {
		// TODO Auto-generated method stub
		if (args.length == 0) {
			System.err.println("Usage: java XPathQuery url");
		    return;
		}
		
		// this is where the HtmlCleaner comes in, I initialize it here
        HtmlCleaner cleaner = new HtmlCleaner();
        CleanerProperties props = cleaner.getProperties();
        props.setAllowHtmlInsideAttributes(true);
        props.setAllowMultiWordAttributes(true);
        props.setRecognizeUnicodeChars(true);
        props.setOmitComments(true);
        
        TagNode node = cleaner.clean(new URL(args[0]));
        //TagNode node = cleaner.clean(args[0]);
		
		Object[] div_nodes = node.evaluateXPath(ITEMAUTO_XPATH);
				
		for (int i = 0; i < div_nodes.length; i++) {
			TagNode div_node = (TagNode) div_nodes[i];
			Object[] sentido = div_node.evaluateXPath("//div[@class='transitoDescripcion']");
			System.out.println(div_node.getText());
			for (int j = 0; j < sentido.length; j++) {				
				String string_sentido = ((TagNode) sentido[j]).getText().toString();
				Object[] estado = ((TagNode)sentido[j]).evaluateXPath("//b");
				String string_estado = ((TagNode)estado[0]).getText().toString();
				System.out.println(string_sentido+": "+string_estado);				
				Object[] comentario = ((TagNode)sentido[0]).evaluateXPath("//span/span");
				if (comentario.length > 0) {
					System.out.println(((TagNode)comentario[0]).getText());
				}												
			}			
		}

        

	}

}
